package com.mmlc.meimei.lichengkongjian.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.mmlc.inter.InterAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.brand.BrandInfoActivity;
import com.mmlc.network.responsebean.CategoryListResponseBean;
import com.mmlc.widget.ExpendClassify;

import java.util.List;

public class ClassifyAdapter extends InterAdapter<CategoryListResponseBean.DataBean.ListBean> {

    ListView list_classify;

    public ClassifyAdapter(Context context, List<CategoryListResponseBean.DataBean.ListBean> datas) {
        super(context, datas);
        list_classify = new ListView(context);
    }

    public ClassifyAdapter(Context context, List<CategoryListResponseBean.DataBean.ListBean> datas, ListView list_classify) {
        super(context, datas);
        this.list_classify = list_classify;
    }

    @Override
    public View getView(final int position, View viewContainer, final ViewGroup parent) {
        ExpendClassify expend_classify = (ExpendClassify) LayoutInflater.from(context).inflate(R.layout.item_classify, parent, false);

        if (select == position && isExtend) {
            expend_classify.toExpend(true, false);
        } else expend_classify.toExpend(false, false);

        expend_classify.setDatas(datas.get(position).getName(), datas.get(position).getLogo(), datas.get(position).getList(),
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
                        Intent intent = new Intent(context, BrandInfoActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                .putExtra("category", datas.get(position).getName())
                                .putExtra("categoryId", datas.get(position).getId())
                                .putExtra("name", datas.get(position).getList().get(index).getName())
                                .putExtra("cate_id", datas.get(position).getList().get(index).getId());
                        context.startActivity(intent);
                    }
                });
        expend_classify.setOnExtendListener(new ExpendClassify.OnExpendListener() {
            @Override
            public void extend(ExpendClassify classify) {
                select = position;
                isExtend = true;

                for (int i = 0; i < list_classify.getChildCount(); i++) {
                    if (i + list_classify.getFirstVisiblePosition() == position) continue;

                    ExpendClassify expend = (ExpendClassify) list_classify.getChildAt(i);
                    if (i + list_classify.getFirstVisiblePosition() != position && expend != null && expend.isExtend())
                        expend.toExpend(false);
                }
            }

            @Override
            public void extendEnd() {
            }

            @Override
            public void shrink(ExpendClassify classify) {
            }

            @Override
            public void shrinkEnd() {

            }
        });

        expend_classify.setTag("expend");
        return expend_classify;
    }

    int select = -1;
    boolean isExtend = false;

}

